package org.mozhu.mboot.admin;

import org.mozhu.mboot.core.security.IdentifiedUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class AppUtils {

    public static IdentifiedUserDetails getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        }
        Object principal = authentication.getPrincipal();
        if (principal != null && principal instanceof IdentifiedUserDetails) {
            return (IdentifiedUserDetails) principal;
        }
        return null;
    }

    public static Long getCurrentUserId() {
        IdentifiedUserDetails userDetails = getCurrentUser();
        return userDetails != null ? userDetails.getUserId() : null;
    }
}
